



*Create local dowstream and upstream exposure
**************************************************

*Input pairwise ij matrix of leontief coefficients. These are calculated as linkages B_ij=(I-A)^-1 from Forsell (1956). See paper for further details. 
use "$path\downstream_leontief_2digit.dta" , clear



preserve
use "$temp\repadata_scaled.dta" , clear
rename ind39 isic
destring isic, replace
keep isic repa_scaled
tempfile repa
save `repa'
restore


preserve
keep isic
gen ind=_n
tempfile key
rename isic ind39
save `key'
restore


reshape long B, i(isic) j(ind)


joinby ind using `key'
drop ind 
destring ind39 isic, replace
rename B D
levelsof ind, local(levels)



rename ind39 isic2
rename isic ind39
rename isic2 isic

*Code differences
replace ind39=3 if ind39==4
replace ind39=4 if ind39==2
replace ind39=2 if ind39==3


tempfile first
save `first'

*Input pairwise ij matrix of leontief coefficients.
use "$path\upstream_leontief_2digit.dta" , clear

preserve
keep isic
gen ind=_n
tempfile key
rename isic ind39
save `key'
restore


reshape long B, i(isic) j(ind)

joinby ind using `key'
drop ind 
destring ind39 isic, replace
rename B U
levelsof ind, local(levels)


joinby  isic ind39 using `first'

replace U=U-1 if isic==ind39
replace D=D-1 if isic==ind39


joinby  ind39 using  "$temp\Long_shares.dta" 

preserve
use "$path\repadata_scaled.dta"  , clear
rename ind39 isic
destring isic, replace
keep isic repa_scaled reparations
tempfile repa
save `repa'
restore

*Non-primary industries only
drop if ind39<5

*Join with the reparations shock. 
joinby isic using `repa'

keep if repa_scaled>0

*Calculate upstream and dowstream shocks.

gen bartik1=U*repa_scaled*share

gen bartik2=D*repa_scaled*share

gen high=(repa_scaled>.5)
gen low=(repa_scaled<.5)

gen bartik_l=bartik1*low
gen bartik_h=bartik1*high

gen bartik_l2=bartik2*low
gen bartik_h2=bartik2*high

gen U_h=U
replace U_h=0 if high==0

gen U_l=U
replace U_l=0 if high==1

gen D_h=D
replace D_h=0 if high==0

gen D_l=D
replace D_l=0 if high==1

gen ind1=(repa_scaled>0)

collapse (sum) ind1 U D D_l D_h U_l U_h  bartik_h* bartik_l*  bartik1 bartik2, by(mun39)


*Define the 1939 local shift share measures.
gen bartik_u=bartik1/U

gen bartik_d=bartik2/D

gen bartik_uh=bartik_h/U

gen bartik_dh=bartik_h2/D

gen bartik_ul=bartik_l/U

gen bartik_dl=bartik_l2/D


*Standardise values in the same sample as the main data.

preserve
use "$temp\municipality_shares_bartik_1939", clear
destring mun39, replace
tempfile sample
keep mun39
gen sample=1
save `sample'
restore

count
joinby mun39 using `sample'
count

egen bartik_u_st=std(bartik_u)
egen bartik_d_st=std(bartik_d)
egen bartik_uh_st=std(bartik_uh)
egen bartik_dh_st=std(bartik_dh)
egen bartik_ul_st=std(bartik_ul)
egen bartik_dl_st=std(bartik_dl)

save "$temp\bartik_down_up_leontief.dta", replace
